package com.jvtd.demo.im;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.jvtd.base.JvtdActivity;
import com.jvtd.demo.R;
import com.jvtd.rong_im.JvtdRongIM;
import com.jvtd.rong_im.mInterface.RongIMConnectCallback;

import java.util.HashMap;
import java.util.Map;

import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;

/**
 * 融云初始化Activity
 * Create by Jack Zhang
 */
public class ImInitActivity extends JvtdActivity implements View.OnClickListener
{
  private static final String TAG = "ImInitActivity";

  private static final int TOKEN_INCORRECT = 0x01;
  private static final int CONNECT_SUCCESS = 0x02;
  private static final int CONNECT_ERROR = 0x03;

  @SuppressLint("HandlerLeak")
  private Handler mHandler = new Handler()
  {
    @Override
    public void handleMessage(Message msg)
    {
      switch (msg.what)
      {
        case TOKEN_INCORRECT:
          Toast.makeText(ImInitActivity.this, "Token 错误", Toast.LENGTH_SHORT).show();
          break;

        case CONNECT_SUCCESS:
          Map<String, Boolean> map = new HashMap<>();
          map.put(Conversation.ConversationType.PRIVATE.getName(), false); // 会话列表需要显示私聊会话, 第二个参数 true 代表私聊会话需要聚合显示
          map.put(Conversation.ConversationType.GROUP.getName(), true);  // 会话列表需要显示群组会话, 第二个参数 false 代表群组会话不需要聚合显示
          JvtdRongIM.getInstance(ImInitActivity.this).startConversationList(ImInitActivity.this, map);
          break;

        case CONNECT_ERROR:
          Toast.makeText(ImInitActivity.this, "连接融云失败", Toast.LENGTH_SHORT).show();
          break;
      }
    }
  };

  @Override
  protected void bindView()
  {
    Toolbar toolbar = findViewById(R.id.include);
    setToolbar(toolbar, true);

    TextView tvTitle = findViewById(R.id.tv_title);
    tvTitle.setText("融云IM");

    Button btnConnect = findViewById(R.id.btn_connect);
    Button btnConnect2 = findViewById(R.id.btn_connect2);
    Button btnDisconnect = findViewById(R.id.btn_disconnect);
    Button btnLogout = findViewById(R.id.btn_logout);
    btnConnect.setOnClickListener(this);
    btnConnect2.setOnClickListener(this);
    btnDisconnect.setOnClickListener(this);
    btnLogout.setOnClickListener(this);
  }

  @Override
  protected void getLayout()
  {
    setContentView(R.layout.activity_im_init);
  }

  @Override
  protected void initViewAndData()
  {

  }

  @Override
  public void onClick(View view)
  {
    switch (view.getId())
    {
      case R.id.btn_connect:
        // userid: user001 name: 用户001
        String token = "CfwhTLhRaQGlwB0nsoBwpcr9vrSk+13sgqDqYDDKd3sFbRvG8XBF2kU/CqtrUbWj8nf35WyZufpm7wyEnLdJCQ==";
        JvtdRongIM.getInstance(this).connect(token, new RongIMConnectCallback()
        {
          @Override
          public void onTokenIncorrect()
          {
            mHandler.sendEmptyMessage(TOKEN_INCORRECT);
          }

          @Override
          public void onSuccess(String userid)
          {
            mHandler.sendEmptyMessage(CONNECT_SUCCESS);
          }

          @Override
          public void onError(RongIMClient.ErrorCode errorCode)
          {
            mHandler.sendEmptyMessage(CONNECT_ERROR);
          }
        });
        break;

      case R.id.btn_connect2:
        // userid: user002 name: 用户002
        String token2 = "Sknvzq9PnrJ3Ur6MQy3UdoJbbu+pzT+VBhCQ+AJM9GJoHugcG07AkyxZU/2iwOWuz1yHLDpya/gpvK212F8r6JzBInIKbxdC";
        JvtdRongIM.getInstance(this).connect(token2, new RongIMConnectCallback()
        {
          @Override
          public void onTokenIncorrect()
          {
            mHandler.sendEmptyMessage(TOKEN_INCORRECT);
          }

          @Override
          public void onSuccess(String userid)
          {
            mHandler.sendEmptyMessage(CONNECT_SUCCESS);
          }

          @Override
          public void onError(RongIMClient.ErrorCode errorCode)
          {
            mHandler.sendEmptyMessage(CONNECT_ERROR);
          }
        });
        break;

      case R.id.btn_disconnect:
        JvtdRongIM.getInstance(this).disconnect();
        break;

      case R.id.btn_logout:
        JvtdRongIM.getInstance(this).logout();
        finish();
        break;
    }
  }
}
